package shuai.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import shuai.model.User;

public class Demo {
	Session session=null;
	
	@Before
	public void initSession(){
		Configuration cfg=new Configuration().configure();
		SessionFactory factory=cfg.buildSessionFactory();
		session=factory.openSession();
	}
	@Test
	public void testAdd(){
		Transaction transaction=session.beginTransaction();
		User user=new User("shuai","11111",11111);
		session.save(user);
		transaction.commit();
	}
	@Test
	public void testGetById(){
		User user=(User) session.get(User.class, 2);
		System.out.println("姓名:"+user.getName());
		System.out.println("年龄："+user.getAge());
		User user2=(User) session.get(User.class, 1);
		System.out.println("姓名:"+user2.getName());
		System.out.println("年龄："+user2.getAge());
	}
	@Test
	public void testUpdate(){
		Transaction transaction=session.beginTransaction();
		User user=(User) session.get(User.class, 1);
		user.setName("王五");
		session.save(user);
		transaction.commit();
	}
	@Test
	public void testDel(){
		Transaction transaction=session.beginTransaction();
		User user=(User) session.get(User.class, 2);
		session.delete(user);
		transaction.commit();
	}
	
	@After
	public void closeSession(){
		session.close();
	}
}
